package com.openlogic.common.logic.node.parse.node;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

/**
* @ClassName: AreaNodeParaseHandler
* @Description: 循环域节点解析处理器
* @author weiyongpeng
* @date 2024年7月14日 下午2:49:12
 */
public class AreaNodeParaseHandler extends BaseSubNodeParseHandler {
	
	@Override
	public List<List<?>> getElementByNode(JSONObject tmpObject, List<List<?>> resultList, Map<String, String> areMap,
			JSONObject lineObject) {
		List<Object> list=new ArrayList<Object>();
		String areaId=tmpObject.get("id").toString();
		JSONObject object=JSON.parseObject(tmpObject.get("data").toString());
		Iterator<String> keys = object.keySet().iterator();  
        while(keys.hasNext()){ 
        	list=new ArrayList<Object>();
        	String key = keys.next().toString();
        	if(!StringUtils.equals("parent", key)) {
        		JSONObject tmp=JSON.parseObject(object.getString(key));
        		String currRuleId=tmp.get("id").toString();
    			list.add(currRuleId); // 获取当前节点id
    			list.add(tmp.get("name") == null ? "" : tmp.get("name").toString()); // 获取name
    			list.add("area"); // 当前节点的所属节点的类型
    			if("switch".equals(tmp.get("type").toString())){ // 这里违背了单一职责，后期优化
    				list.add(getSwitchDataInfo(tmp.getJSONObject("data")));
    			}else{
    				list.add(tmp.get("data") == null ? tmp.get("data") : getDataInfo(tmp.get("data").toString()));
    			}
    			list.add(areaId);
    			list.add(tmp.get("type").toString());
    			boolean flag=false;
    			if(areMap.get("E_"+areaId)!=null&&!"".equals(areMap.get("E_"+areaId))){ //判断是否是循环域中的最后一个节点点
    				if(areMap.get("E_"+areaId).indexOf(currRuleId+"#")>-1){
    					flag=true;
    				}
    			}
    			list.add(flag);
    			list.add(getlistMap(lineObject,currRuleId));//添加该节点对应向下线路的id
//    			list.add(tmpObject.get("dataBind") == null ? Maps.newHashMap() : getDataBind(tmpObject.get("dataBind").toString())); // 获取节点绑定变量的数据
    			resultList.add(list);
        	}
        }
		return resultList;
	}

	private Object getSwitchDataInfo(JSONObject tmpObject) {
		Map<String, String> map = new HashMap<String, String>();

		String value = tmpObject.get("value") == null ? "" : tmpObject.get("value").toString();
		map.put("value", value);

		String type = tmpObject.get("type") == null ? "" : tmpObject.get("type").toString();
		map.put("type", type);

		String text = tmpObject.get("text") == null ? "" : tmpObject.get("text").toString();
		map.put("text", text);

		return map;
	}

}
